package com.itheima.contrall;

import com.itheima.bean.Course;
import com.itheima.service.AdminService;

import java.io.IOException;
import java.util.Scanner;

public class AdminContrall {
    Scanner sc = new Scanner(System.in);
    private AdminService adminService = new AdminService();

    public void start() throws IOException {
          while (true) {
              System.out.println("请输入管理员id：");
              String adminid = sc.next();
              System.out.println("请输入管理员密码：");
              String apassword = sc.next();
              AdminService adminService = new AdminService();
              boolean flag = adminService.istrue(adminid, apassword);
              if (flag) {
                  System.out.println("管理员登录成功");
                  break;

              } else {
                  System.out.println("登录失败，重新输入");
              }
          }
            teacherloop:  while (true){
        System.out.println("--------------欢迎管理员进入-----------------");
        System.out.println("请选择：1.添加课程  2.删除课程  3.查看课程  4.退出");
        String choose = sc.next();

        switch (choose) {
            case "1":
                addcourse();
                break;
            case "2":
                deletecourse();
                break;
            case "3":
                findcourse();
                break;
            case "4":
                System.out.println("退出");
                break teacherloop;
            default:
                System.out.println("输入错误，重新输入");
        }
    }
    }

    public void findcourse() throws IOException {
     adminService.findcourse();

    }

    public void deletecourse() throws IOException {
        String id;
        while (true) {
            System.out.println("请输入删除的课程编号");
            id = sc.next();
            boolean flag = adminService.isExits(id);
            if (!flag) {
                System.out.println("您输入的课程编号错误，重新输入");
            } else {
                break;
            }
        }
        adminService.deletecourse(id);
        System.out.println("删除成功");
    }

    public void addcourse() throws IOException {
        String id;
        while (true) {
            System.out.println("请输入课程编号");
            id = sc.next();
            boolean flag = adminService.isExits(id);
            if (flag) {
                System.out.println("编号冲突，重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入课程名称");
        String name = sc.next();
        System.out.println("请输入课程类型");
        String type = sc.next();
        Course course = new Course(id, name, type);

        boolean reault = adminService.addcourse(course);
        if (reault) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}
